function PLUGIN:Initialize() 

end

-- Set mass
local function SetMass( Player, Entity, Data )
	if not SERVER then return end
	if Data.Mass then
		local physobj = Entity:GetPhysicsObject()
		if physobj:IsValid() then physobj:SetMass(Data.Mass) end
	end
	duplicator.StoreEntityModifier( Entity, "mass", Data )
end
duplicator.RegisterEntityModifier( "mass", SetMass )

function PLUGIN:setMass(ply, args)
	if CLIENT then return end

	local ent = ply:GetEyeTraceNoCursor().Entity
	local mass = math.Clamp(tonumber(args[1]), 1, 50000)

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			SetMass(ply , ent , {Mass = mass})
		end
	end	
	if ecs.CanManipulate(ply, ent) then
		SetMass(ply , ent , {Mass = mass})
	end
end
PLUGIN:AddServerCommand( "mass" , PLUGIN.setMass )


-- Toggle gravity
function PLUGIN:setGravity(ply, args)
	if CLIENT then return end

	local ent = ply:GetEyeTraceNoCursor().Entity
	local gravity = tobool(args[1])

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			construct.SetPhysProp( ply , ent, 0, nil,  {GravityToggle = gravity})
		end
	end
	if ecs.CanManipulate(ply, ent) then
		construct.SetPhysProp(ply, ent, 0, nil, {GravityToggle = gravity})
	end
end
PLUGIN:AddServerCommand( "gravity" , PLUGIN.setGravity )


-- Toggle drag
local function enabledrag(_ , Entity , Data )
	if SERVER then
		if Entity:IsValid() and !Entity:IsWorld() then
			local phys = Entity:GetPhysicsObject()
			phys:EnableDrag( Data.DragOnOff )
		end
		duplicator.StoreEntityModifier( Entity, "DragEnabled" , Data)
	end
end
duplicator.RegisterEntityModifier( "DragEnabled", enabledrag )

function PLUGIN:setDrag(ply, args)
	if CLIENT then return end
	
	local ent = ply:GetEyeTraceNoCursor().Entity
	local drag = tobool(args[1])

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			enabledrag(ply, ent, {DragOnOff = drag})
		end
	end
	if ecs.CanManipulate(ply, ent) then
		enabledrag(ply, ent, {DragOnOff = drag})
	end
end
PLUGIN:AddServerCommand( "drag" , PLUGIN.setDrag )


-- Set color
local function SetColour( Player, Entity, Data )
	if ( Data.Color ) then Entity:SetColor( Color( Data.Color.r, Data.Color.g, Data.Color.b, Data.Color.a ) ) end
	if ( Data.RenderMode ) then Entity:SetRenderMode( Data.RenderMode ) end
	if ( Data.RenderFX ) then Entity:SetKeyValue( "renderfx", Data.RenderFX ) end

	if SERVER then
		duplicator.StoreEntityModifier( Entity, "colour", Data )
	end
end
duplicator.RegisterEntityModifier( "colour", SetColour )

function PLUGIN:setColor(ply, args)
	if CLIENT then return end

	local ent = ply:GetEyeTraceNoCursor().Entity
	local color = Color(args[1], args[2], args[3], args[4]) -- errors if arguments aren't provided?
	local oldcolor

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			oldcolor = Color(ent:GetColor()) 
			SetColour(ply, ent, {Color = color })
			ecs.OldColours[ent] = color
			ent:SetColor(oldcolor)
		end
	end
	if ecs.CanManipulate(ply, ent) then
		SetColour(ply, ent, {Color = color })
	end
end
PLUGIN:AddServerCommand( "color" , PLUGIN.setColor )


-- Set material
local function SetMaterial( Player, Entity, Data )
	if SERVER then
		Entity:SetMaterial( Data.MaterialOverride )
		duplicator.StoreEntityModifier( Entity, "material", Data )
	end
	return true
end
duplicator.RegisterEntityModifier( "material", SetMaterial )

function PLUGIN:setMaterial(ply, args)
	if CLIENT then return end

	local ent = ply:GetEyeTraceNoCursor().Entity
	local material = args[1]

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			SetMaterial(ply, ent , {MaterialOverride = material})
		end
	end
	if ecs.CanManipulate(ply, ent) then
		SetMaterial(ply, ent , {MaterialOverride = material})
	end
end
PLUGIN:AddServerCommand( "material" , PLUGIN.setMaterial )


-- Set surface properties
function PLUGIN:setPhysprop(ply, args)
	if CLIENT then return end

	local ent = ply:GetEyeTraceNoCursor().Entity
	local mat = tostring(args[1])

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			construct.SetPhysProp(ply , ent, 0, nil, {GravityToggle = true, Material = mat})
		end
	end
	if ecs.CanManipulate(ply, ent) then
		construct.SetPhysProp(ply, ent, 0, nil, {GravityToggle = true, Material = mat})
	end
end
PLUGIN:AddServerCommand( "physprop" , PLUGIN.setPhysprop )


-- Freeze
function PLUGIN:freezeProp(ply, args)
	if CLIENT then return end

	local ent = ply:GetEyeTraceNoCursor().Entity
	local physobj

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			physobj = ent:GetPhysicsObject()
			if physobj:IsValid() then
				physobj:EnableMotion( false )
			end
		end
	end
	if ecs.CanManipulate(ply, ent) then
		physobj = ent:GetPhysicsObject()
		if physobj:IsValid() then
			physobj:EnableMotion( false )
		end
	end
end
PLUGIN:AddServerCommand( "freeze" , PLUGIN.freezeProp )

-- Freeze
function PLUGIN:unfreezeProp(ply, args)
	if CLIENT then return end

	local ent = ply:GetEyeTraceNoCursor().Entity
	local physobj

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			physobj = ent:GetPhysicsObject()
			if physobj:IsValid() then
				physobj:EnableMotion( true )
				physobj:Wake()
			end
		end
	end
	if ecs.CanManipulate(ply, ent) then
		physobj = ent:GetPhysicsObject()
		if physobj:IsValid() then
			physobj:EnableMotion( true )
			physobj:Wake()
		end
	end
end
PLUGIN:AddServerCommand( "unfreeze" , PLUGIN.unfreezeProp )


-- Set buoyancy
-- local function SetBuoyancy(Player, Entity, Data)
	-- if SERVER then 
		-- local ratio = Data.Ratio
		-- local physobj = Entity:GetPhysicsObject()
		
		-- if physobj:IsValid() then 
			-- local ratio = mat.clamp(Data.Ratio, -1000, 1000) * 0.01
			-- Entity.BuoyancyRatio = ratio
			-- physobj:SetBuoyancyRatio(ratio)
			-- physobj:Wake()
			
			-- duplicator.StoreEntityModifier(Entity, "buoyancy", Data)
		-- end
		-- return true
	-- end
-- end
-- duplicator.RegisterEntityModifier("buoyancy", SetBuoyancy)

-- function PLUGIN:setBuoyancy(ply, args)
	-- if CLIENT then return end

	-- local ent = ply:GetEyeTraceNoCursor().Entity
	-- local buoy = args[1]

	-- if ecs.Selections[ply] then
		-- for k, ent in pairs(ecs.Selections[ply]) do
			-- SetBuoyancy(ply, ent, buoy)
		-- end
	-- end
	-- if ecs.CanManipulate(ply, ent) then
		-- SetBuoyancy(ply, ent, buoy)
	-- end
-- end
-- PLUGIN:AddServerCommand( "buoyancy" , PLUGIN.setBuoyancy )